import { problemList } from '@/static/js/problem.js';
import { formatTime } from '@/utils/utils.js';
export default {
  data() {
    return {
      isPullDown: false,//是否下拉刷新
      tabList: [
        { name: '发布时间', type: 'timeup' },
        { name: '浏览量', type: 'hotup' },
      ],
      page: 3,//当前页码
      limit: 10,//每页条数
      chooseType: 'timeup',//当前选中的tab的type
      list: [],//问题列表
      loadMoreStatus: 'more',//加载更多
    };
  },
  onLoad(e) {
    this.getProblemList();
  },
  methods: {
    // 切换tab
    toggle(e) {
      this.chooseType = e.type;
      this.page = 3;
      this.list = [];
      this.getProblemList();
    },

    //请求问题列表
    getProblemList() {
      let params = {
        page: this.page,
        limit: this.limit,
        type: this.chooseType,
        grade_switch: 0,
        cate_id: 0,
      };
      problemList(params).then(res => {
        if (res.code == 1) {
          let list = res.data;
          if (list.length < this.limit) {
            this.loadMoreStatus = 'noMore';
          }
          list.forEach(e => {
            e.createTime = formatTime(e.createtime * 1000, 'Y-M-D')
            e.answer_content = e.answer_content.replace(/<[^>]*>/g, "")
          });
          console.log(list,'list');
          this.list = this.list.concat(list);
          this.isPullDown = false
        }
      });
    },

    //去详情
    go(e) {
      uni.navigateTo({ url: `/library/problem/details/index?id=${e.id}` })
    },

    //去搜索
    goSearch() {
      if (!uni.getStorageSync('token')) {
        uni.navigateTo({ url: '/pages/login/login' })
        return;
      }
      uni.navigateTo({ url: '/library/problem/search/index' })
    },


    //下拉刷新
    pullDownFnc() {
      this.isPullDown = true;
      this.page = 3;
      this.list = [];
      this.getProblemList();
    },
    //上拉加载
    loadMore() {
      if (this.loadMoreStatus == 'noMore') return;
      this.page++;
      this.getProblemList();
    },
  },
}